import { message } from 'antd'
import { queryPersonal, projectFinish, projectRemarkList, projectRemarkEdit } from '../services/personal'

export default {
  namespace: 'staffPersonal',

  state: {
    visible: false,
    task_id: 0,
    remark: '',

    list: []
  },

  effects: {
    // 获取员工列表
    * fetchPersonal({ payload = {} }, { call, put, select }) {
      const { status = false, data = [] } = yield call(queryPersonal, payload)
      let list = []
      if (status) {
        list = data
      }
      yield put({
        type: 'save',
        payload: { list }
      })
    },
    // 项目备注展示
    * projectRemarkList({ payload = {} }, { call, put }) {
      const { status = false, data = {} } = yield call(projectRemarkList, payload)
      if (status) {
        yield put({
          type: 'save',
          payload: { remark: data.task_remark }
        })
      } else {
        message.error('获取项目备注失败')
      }

    },
    // 项目完成提交
    * projectFinish({ payload = {} }, { call, put }) {
      const { status = false, errcode = 0 } = yield call(projectFinish, payload)
      if (status) {
        message.success('任务提交成功')
        yield put({
          type: 'fetchPersonal',
        })
      } else {
        if (errcode === 4) {
          message.error('提交时间超过项目开始立项时间，提交失败')
        } else {
          message.error('任务提交失败')
        }
      }

    },
    // 项目备注提交
    * projectRemarkEdit({ payload = {} }, { call, put }) {
      const { status = false } = yield call(projectRemarkEdit, payload)
      if (status) {
        message.success('任务备注成功')
        yield put({
          type: 'save',
          payload: {
            visible: false
          },
        })
      } else {
        message.success('任务备注失败')
      }

    },

  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },
  },
}